from numpy import array,append,zeros,ones,amin
import sys




def findElementInList(list_element, element):
        try:
            index_element=list_element.index(element)
            return index_element
        except ValueError:
            return -1

#read two strings separated and split into integers
def readHeader(hdr):
    hdrSplit = hdr.split()
    (m,n) = (int(hdrSplit[0]),int(hdrSplit[1]))
    return (m,n)
# Read a list of integers
def readBasicIndices(bline):
    blineSplit = bline.split()
    return map(int, blineSplit)

# Read a vector of floats
def readVectorLine(hdr):
    hdrSplit=hdr.split()
    return map(float, hdrSplit)

# Read the A matrix
def readAMatrix(f,m,n):
    retMat=[]
    for i in range(1,m+1):
        print 'Reading A matrix row #', i
        rowI= readVectorLine(f.readline())
        assert (len(rowI) == n), 'Badly formatted line reading matrix A'
        retMat.append(rowI)
    return retMat

# Basic function
def readDictionary(fileName):
    f = open(fileName,'r')
    (m,n) = readHeader(f.readline())
    basicIndices = readBasicIndices(f.readline())
    assert (len(basicIndices) == m), 'Wrong number of basic indices'
    nonbasicIndices = readBasicIndices(f.readline())
    assert (len(nonbasicIndices) == n), 'Wrong number of non=basic indices'
    bVec = readVectorLine(f.readline())
    assert (len (bVec) == m), ' b vector has wrong number of entries'
    aMat = readAMatrix(f,m,n)
    cVec = readVectorLine(f.readline())
    assert (len (cVec)== n+1), 'c vector has wrong number of entries' 
    print 'Read Dictionary AOK \n'
    f.close()
    return (m,n,basicIndices, nonbasicIndices, array(bVec),array(cVec),array(aMat))